package com.ruoyi.asset.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.asset.common.AssetStatusType;
import com.ruoyi.asset.common.AssetTransactionRecordType;
import com.ruoyi.asset.domain.FixedAssets;
import com.ruoyi.asset.mapper.FixedAssetsMapper;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.AssetTransactionRecordMapper;
import com.ruoyi.asset.domain.AssetTransactionRecord;
import com.ruoyi.asset.service.IAssetTransactionRecordService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 领用归还记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-06
 */
@Service
public class AssetTransactionRecordServiceImpl extends ServiceImpl<AssetTransactionRecordMapper, AssetTransactionRecord> implements IAssetTransactionRecordService {
    @Autowired
    private AssetTransactionRecordMapper assetTransactionRecordMapper;

    @Autowired
    private FixedAssetsMapper fixedAssetsMapper;

    /**
     * 查询领用归还记录
     *
     * @param transactionId 领用归还记录主键
     * @return 领用归还记录
     */
    @Override
    public AssetTransactionRecord selectAssetTransactionRecordByTransactionId(String transactionId) {
        return assetTransactionRecordMapper.selectById(transactionId);
    }

    /**
     * 查询领用归还记录列表
     *
     * @param assetTransactionRecord 领用归还记录
     * @return 领用归还记录
     */
    @Override
    public List<AssetTransactionRecord> selectAssetTransactionRecordList(AssetTransactionRecord assetTransactionRecord) {
        return assetTransactionRecordMapper.selectAssetTransactionRecordList(assetTransactionRecord);
    }

    /**
     * 新增领用归还记录
     *
     * @param assetTransactionRecord 领用归还记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAssetTransactionRecord(AssetTransactionRecord assetTransactionRecord) {
        //获取资产编号
        String assetNumber = assetTransactionRecord.getAssetNumber();
        //查询资产信息
        FixedAssets fixedAssets = fixedAssetsMapper.selectById(assetNumber);
        String assetStatus = fixedAssets.getAssetStatus();
        if (fixedAssets == null) {
            throw new UtilException("无资产数据！");
        }
        //报废状态无法修改信息
        if (assetStatus.equals(AssetStatusType.SCRAPPED)) {
            throw new UtilException("报废状态无法添加数据");
        }
        //如果是待报废状态下
        if (assetStatus.equals(AssetStatusType.TO_BE_SCRAPPED)) {
            throw new UtilException("待报废状态下不能有有资产转移");
        }
        //如果是维修状态下
        if (assetStatus.equals(AssetStatusType.MAINTENANCE)) {
            throw new UtilException("维修状态下资产不能转移");
        }
        //查询领用借用记录，状态为0或1的条数集合
        LambdaQueryWrapper<AssetTransactionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssetTransactionRecord::getAssetNumber, assetNumber)
                .and(qw -> qw.eq(AssetTransactionRecord::getTransactionType, AssetTransactionRecordType.CLAIM)
                        .or()
                        .eq(AssetTransactionRecord::getTransactionType, AssetTransactionRecordType.BORROW));
        List<AssetTransactionRecord> assetTransactionRecord1 = assetTransactionRecordMapper.selectList(queryWrapper);
        //无借用或领用记录
        if (assetTransactionRecord1.size() > 0) {
            throw new UtilException("此资产非呆滞状态");
        }
//        String pattern = "^(0\\d{5}$)";
        //生成交易编号
        String s = UUID.randomUUID().toString();
        if (StringUtils.isEmpty(assetTransactionRecord.getTransactionId())) {
            assetTransactionRecord.setTransactionId(s);
        }
//        if (!assetTransactionRecord.getBorrower().matches(pattern)) {
//            throw new UtilException("使用人异常");
//        }
        if (assetTransactionRecord.getTransactionDate() == null) {
            //生成交易日期
            assetTransactionRecord.setTransactionDate(new Date());
        }
        fixedAssets.setUserId(assetTransactionRecord.getBorrower());
        fixedAssets.setUserName(assetTransactionRecord.getBorrowerName());
        fixedAssets.setDepartment(assetTransactionRecord.getDepartment());
        fixedAssets.setAssetStatus(assetTransactionRecord.getTransactionType());
        //是否归还状态
        if (assetTransactionRecord.getTransactionType().equals(AssetTransactionRecordType.SLUGGISH)) {
            //必须填写归还人和归还时间
            if (StringUtils.isEmpty(assetTransactionRecord.getReturner()) || assetTransactionRecord.getReturnDate() == null) {
                throw new UtilException("请填写归还日期归还人！");
            }
//            if (!assetTransactionRecord.getReturner().matches(pattern)) {
//                throw new UtilException("归还人异常");
//            }
            //判断日期
            if (1 == assetTransactionRecord.getTransactionDate().compareTo(assetTransactionRecord.getReturnDate())) {
                throw new UtilException("时间填写错误！");
            }
            fixedAssets.setUserId("无");
            fixedAssets.setUserName("");
            fixedAssets.setDepartment("");
            fixedAssets.setAssetStatus(assetTransactionRecord.getTransactionType());
        }
        fixedAssetsMapper.updateById(fixedAssets);
        return assetTransactionRecordMapper.insert(assetTransactionRecord);
    }

    /**
     * 修改领用归还记录
     *
     * @param assetTransactionRecord 领用归还记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAssetTransactionRecord(AssetTransactionRecord assetTransactionRecord) {
        String assetNumber = assetTransactionRecord.getAssetNumber();
        FixedAssets fixedAssets = fixedAssetsMapper.selectById(assetNumber);
        String assetStatus = fixedAssets.getAssetStatus();
        //报废状态无法修改信息
        if (assetStatus.equals(AssetStatusType.SCRAPPED)) {
            throw new UtilException("报废状态无法添加数据");
        }
        //如果是待报废状态下
        if (assetStatus.equals(AssetStatusType.TO_BE_SCRAPPED)) {
            throw new UtilException("待报废状态下不能有有资产转移");
        }
        //如果是维修状态下
        if (assetStatus.equals(AssetStatusType.MAINTENANCE)) {
            throw new UtilException("维修状态下资产不能转移");
        }
        //获取交易类型
        AssetTransactionRecord transactionRecord = assetTransactionRecordMapper.selectById(assetTransactionRecord.getTransactionId());
        String transactionType = transactionRecord.getTransactionType();
//        String pattern = "^(0\\d{5}$)";

//        if (!assetTransactionRecord.getBorrower().matches(pattern)) {
//            throw new UtilException("借用人异常");
//        }
        fixedAssets.setAssetNumber(assetNumber);
        //借用||领用
        if (transactionType.equals(AssetTransactionRecordType.CLAIM) || transactionType.equals(AssetTransactionRecordType.BORROW)) {
            if ((StringUtils.isNotEmpty(assetTransactionRecord.getReturner()) && assetTransactionRecord.getReturnDate() == null) || (StringUtils.isEmpty(assetTransactionRecord.getReturner()) && !(assetTransactionRecord.getReturnDate() == null))) {
                throw new UtilException("归还日期和归还人须同时填写！");
            }
            //有归还日期和归还人视为归还
            if (StringUtils.isNotEmpty(assetTransactionRecord.getReturner()) && !(assetTransactionRecord.getReturnDate() == null)) {
                //判断日期
                if (1 == assetTransactionRecord.getTransactionDate().compareTo(assetTransactionRecord.getReturnDate())) {
                    throw new UtilException("时间填写错误！");
                }
                assetTransactionRecord.setTransactionType(AssetTransactionRecordType.SLUGGISH);
                fixedAssets.setUserId("无");
                fixedAssets.setUserName("");
                fixedAssets.setDepartment("");
                fixedAssets.setAssetStatus(assetTransactionRecord.getTransactionType());
            } else {
                fixedAssets.setUserId(assetTransactionRecord.getBorrower());
                fixedAssets.setUserName(assetTransactionRecord.getBorrowerName());
                fixedAssets.setDepartment(assetTransactionRecord.getDepartment());
                fixedAssets.setAssetStatus(assetTransactionRecord.getTransactionType());
            }
        }
        //归还
        if (transactionType.equals(AssetTransactionRecordType.SLUGGISH)) {
            //判断归还日期和归还人
            if (StringUtils.isEmpty(assetTransactionRecord.getReturner()) || assetTransactionRecord.getReturnDate() == null) {
                throw new UtilException("归还人和归还时间不可为空！");
            }
//            if (!assetTransactionRecord.getReturner().matches(pattern)) {
//                throw new UtilException("归还人异常！");
//            }


            //判断日期
            if (1 == assetTransactionRecord.getTransactionDate().compareTo(assetTransactionRecord.getReturnDate())) {
                throw new UtilException("时间填写错误！");
            }
            fixedAssets.setUserId("无");
            fixedAssets.setUserName("");
            fixedAssets.setDepartment("");
            fixedAssets.setAssetStatus(transactionType);
        }
        fixedAssetsMapper.updateById(fixedAssets);
        return assetTransactionRecordMapper.updateById(assetTransactionRecord);
    }


    /**
     * 批量删除领用归还记录
     *
     * @param transactionIds 需要删除的领用归还记录主键
     * @return 结果
     */
    @Override
    public int deleteAssetTransactionRecordByTransactionIds(String transactionIds) {
        String[] transactionId = Convert.toStrArray(transactionIds);
        for (int i = 0; i < transactionId.length; i++) {
            AssetTransactionRecord transactionRecord = assetTransactionRecordMapper.selectById(transactionId[i]);
            String transactionType = transactionRecord.getTransactionType();
            if (transactionType.equals(AssetTransactionRecordType.CLAIM) || transactionType.equals(AssetTransactionRecordType.BORROW)) {
                FixedAssets fixedAssets = new FixedAssets();
                fixedAssets.setAssetNumber(transactionRecord.getAssetNumber());
                fixedAssets.setUserId("无");
                fixedAssets.setUserName("");
                fixedAssets.setDepartment("");
                fixedAssetsMapper.updateById(fixedAssets);
            }
        }
        return assetTransactionRecordMapper.deleteAssetTransactionRecordByTransactionIds(transactionId);
    }

    /**
     * 删除领用归还记录信息
     *
     * @param transactionId 领用归还记录主键
     * @return 结果
     */
    @Override
    public int deleteAssetTransactionRecordByTransactionId(String transactionId) {
        AssetTransactionRecord transactionRecord = assetTransactionRecordMapper.selectById(transactionId);
        String transactionType = transactionRecord.getTransactionType();
        if (transactionType.equals(AssetTransactionRecordType.CLAIM) || transactionType.equals(AssetTransactionRecordType.BORROW)) {
            FixedAssets fixedAssets = new FixedAssets();
            fixedAssets.setAssetNumber(transactionRecord.getAssetNumber());
            fixedAssets.setUserId("无");
            fixedAssets.setUserName("");
            fixedAssets.setDepartment("");
            fixedAssetsMapper.updateById(fixedAssets);
        }
        return assetTransactionRecordMapper.deleteById(transactionId);
    }

    /**
     * 根据资产编号获取资产名称
     **/
    @Override
    public String getAssetName(String assetNumber) {
        String assetName = fixedAssetsMapper.selectById(assetNumber).getAssetName();
        return assetName;
    }

    @Override
    public String returnAsset(AssetTransactionRecord assetTransactionRecord) {
        String assetNumber = assetTransactionRecord.getAssetNumber();
        FixedAssets fixedAssets = fixedAssetsMapper.selectById(assetNumber);
        String assetStatus = fixedAssets.getAssetStatus();
        //报废状态无法修改信息
        if (assetStatus.equals(AssetStatusType.SCRAPPED)) {
            return "报废状态无法添加数据";
        }
        //如果是待报废状态下
        if (assetStatus.equals(AssetStatusType.TO_BE_SCRAPPED)) {
            return "待报废状态下不能有有资产转移";
        }
        //如果是维修状态下
        if (assetStatus.equals(AssetStatusType.MAINTENANCE)) {
            return "维修状态下资产不能转移";
        }
        //获取交易类型
        AssetTransactionRecord transactionRecord = assetTransactionRecordMapper.selectById(assetTransactionRecord.getTransactionId());
        String transactionType = transactionRecord.getTransactionType();
//        String pattern = "^(0\\d{5}$)";

//        if (!assetTransactionRecord.getBorrower().matches(pattern)) {
//            throw new UtilException("借用人异常");
//        }
        fixedAssets.setAssetNumber(assetNumber);
        //借用||领用
        if (transactionType.equals(AssetTransactionRecordType.CLAIM) || transactionType.equals(AssetTransactionRecordType.BORROW)) {
            if ((StringUtils.isNotEmpty(assetTransactionRecord.getReturner()) && assetTransactionRecord.getReturnDate() == null) || (StringUtils.isEmpty(assetTransactionRecord.getReturner()) && !(assetTransactionRecord.getReturnDate() == null))) {
                return "归还日期和归还人须同时填写！";
            }
            //有归还日期和归还人视为归还
            if (StringUtils.isNotEmpty(assetTransactionRecord.getReturner()) && !(assetTransactionRecord.getReturnDate() == null)) {
                //判断日期
                if (1 == assetTransactionRecord.getTransactionDate().compareTo(assetTransactionRecord.getReturnDate())) {
                    return "时间填写错误！";
                }
                assetTransactionRecord.setTransactionType(AssetTransactionRecordType.SLUGGISH);
                fixedAssets.setUserId("无");
                fixedAssets.setUserName("");
                fixedAssets.setDepartment("");
                fixedAssets.setAssetStatus(assetTransactionRecord.getTransactionType());
            } else {
                fixedAssets.setUserId(assetTransactionRecord.getBorrower());
                fixedAssets.setUserName(assetTransactionRecord.getBorrowerName());
                fixedAssets.setDepartment(assetTransactionRecord.getDepartment());
                fixedAssets.setAssetStatus(assetTransactionRecord.getTransactionType());
            }
        }
        //归还
        if (transactionType.equals(AssetTransactionRecordType.SLUGGISH)) {
            //判断归还日期和归还人
            if (StringUtils.isEmpty(assetTransactionRecord.getReturner()) || assetTransactionRecord.getReturnDate() == null) {
                return "归还人和归还时间不可为空！";
            }
//            if (!assetTransactionRecord.getReturner().matches(pattern)) {
//                throw new UtilException("归还人异常！");
//            }


            //判断日期
            if (1 == assetTransactionRecord.getTransactionDate().compareTo(assetTransactionRecord.getReturnDate())) {
                return ("时间填写错误！");
            }
            fixedAssets.setUserId("无");
            fixedAssets.setUserName("");
            fixedAssets.setDepartment("");
            fixedAssets.setAssetStatus(transactionType);
        }
        fixedAssetsMapper.updateById(fixedAssets);


        return null;
    }


    @Override
    public String lendAsset(AssetTransactionRecord assetTransactionRecord) {
        //获取资产编号
        String assetNumber = assetTransactionRecord.getAssetNumber();
        //查询资产信息
        FixedAssets fixedAssets = fixedAssetsMapper.selectById(assetNumber);
        String assetStatus = fixedAssets.getAssetStatus();
        if (fixedAssets == null) {
            return "无资产数据！";
        }
        //报废状态无法修改信息
        if (assetStatus.equals(AssetStatusType.SCRAPPED)) {
            return "报废状态无法添加数据";
        }
        //如果是待报废状态下
        if (assetStatus.equals(AssetStatusType.TO_BE_SCRAPPED)) {
            return "待报废状态下不能有有资产转移";
        }
        //如果是维修状态下
        if (assetStatus.equals(AssetStatusType.MAINTENANCE)) {
            return "维修状态下资产不能转移";
        }
        //查询领用借用记录，状态为0或1的条数集合
        LambdaQueryWrapper<AssetTransactionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssetTransactionRecord::getAssetNumber, assetNumber)
                .and(qw -> qw.eq(AssetTransactionRecord::getTransactionType, AssetTransactionRecordType.CLAIM)
                        .or()
                        .eq(AssetTransactionRecord::getTransactionType, AssetTransactionRecordType.BORROW));
        List<AssetTransactionRecord> assetTransactionRecord1 = assetTransactionRecordMapper.selectList(queryWrapper);
        //无借用或领用记录
        if (assetTransactionRecord1.size() > 0) {
            return "此资产非呆滞状态";
        }
//        String pattern = "^(0\\d{5}$)";
        //生成交易编号
        String s = UUID.randomUUID().toString();
        if (StringUtils.isEmpty(assetTransactionRecord.getTransactionId())) {
            assetTransactionRecord.setTransactionId(s);
        }
//        if (!assetTransactionRecord.getBorrower().matches(pattern)) {
//            throw new UtilException("使用人异常");
//        }
        if (assetTransactionRecord.getTransactionDate() == null) {
            //生成交易日期
            assetTransactionRecord.setTransactionDate(new Date());
        }
        fixedAssets.setUserId(assetTransactionRecord.getBorrower());
        fixedAssets.setUserName(assetTransactionRecord.getBorrowerName());
        fixedAssets.setDepartment(assetTransactionRecord.getDepartment());
        fixedAssets.setAssetStatus(assetTransactionRecord.getTransactionType());
        //是否归还状态
        if (assetTransactionRecord.getTransactionType().equals(AssetTransactionRecordType.SLUGGISH)) {
            //必须填写归还人和归还时间
            if (StringUtils.isEmpty(assetTransactionRecord.getReturner()) || assetTransactionRecord.getReturnDate() == null) {
                return "请填写归还日期归还人！";
            }
//            if (!assetTransactionRecord.getReturner().matches(pattern)) {
//                throw new UtilException("归还人异常");
//            }
            //判断日期
            if (1 == assetTransactionRecord.getTransactionDate().compareTo(assetTransactionRecord.getReturnDate())) {
                return "时间填写错误！";
            }
            fixedAssets.setUserId("无");
            fixedAssets.setUserName("");
            fixedAssets.setDepartment("");
            fixedAssets.setAssetStatus(assetTransactionRecord.getTransactionType());
        }
        fixedAssetsMapper.updateById(fixedAssets);
        return null;
    }


}
